home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
PROGRAM
/
BPAS9.ARJ
/
3_2.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1991-09-07
|
10KB
|
265 lines
{---------------------------------------------------------------------
PROGRAM: 3_2
This program uses the template to build a few procedures quickly,
the programmer is able to copy and fill in the NULL.PROC procedure
template with a procedure or function and rename it as needed, moving the
new procedure to its alphabetical place in the procedures, and placing
the procedure name in the MAIN PROGGRAM so that it is called in its
turn.
However, what if the programmer wants to call a procedure or function
when he wants to? What if he does not want the whole program to run
all the time, but wants to give the user options of which part of the
program to run?
What is new in this template is the placing of a menu in the main
program so that the user can call the procedure/function as needed and
as desired.
The program still uses INIT, and USES the CRT Unit to ClrScr
at initiation. It also adds READLN to the end of the program
so that, when it is run, the output is sent to the screen and stops so
that the programmer is allowed to see the output without pressing ALT F5.
Author: Mike Benedict
Date Started: 9/01/91
Latest Revision: 9/01/91
Version: Turbo Pascal 6.0
-------------------------------------------------------------------}
PROGRAM Lesson3_2;
USES { USES is a reserved word that }
Crt; { tells the compiler to use one }
{ of the standard libraries or }
{ units listed in the Pascal }
{ Programmer's Guide. }
VAR
Choice : Char; { Used in CASE Statements }
UserQuits : Boolean; { Used to Quit Menu & Program }
{------------------------------------} { Anything between brackets }
{ PROCEDURES } { is ignored at compile time }
{------------------------------------} { and allows clear comments }
{ within source code. }
{-----------------------------}
{ ASCII.PROC }
{-----------------------------}
PROCEDURE ASCII;
{ See Tom Swan p. 39 ff }
VAR
Ch : Char;
Num : Integer;
BEGIN
ClrScr;
WriteLn;
WriteLn;
Write ( 'ASCII Character Set ' );
WriteLn;
WriteLn;
FOR Num := 0 TO 255 DO
WriteLn ( 'ASCII Number = ',Num, ' Character = ',Char(Num) );
ReadLn;
END;
{ As an exercise, convert this procedure to
one that prints out the ASCII code. See
Tom Swan pp. 587-588 to declare a file,
then make the file LPT1. )
{-----------------------------}
{ INIT.PROC }
{-----------------------------}
PROCEDURE Init;
BEGIN
TextBackground(Blue); { Procedures and functions that }
TextColor(White); { are pre-defined by Borland's }
ClrScr; { Reference manual. They are }
END; { called the Run-Time Library. }
{-----------------------------}
{ RANGECHK.PROC }
{-----------------------------}
PROCEDURE RangeChk;
{ See Tom Swan p. 25 ff }
VAR
Age : 0..100;
BEGIN
ClrScr;
WriteLn;
WriteLn;
Write ( 'How old are you? ' );
{$R+} { Compiler Directive: Turn Range Checking On }
ReadLn ( Age );
{$R-} { Compiler Directive: Turn Range Checking Off }
WriteLn;
WriteLn;
WriteLn ( 'You are ', Age, ' years old.' );
ReadLn;
END;
{-----------------------------}
{ RANGECHK2.PROC }
{-----------------------------}
PROCEDURE RangeChk2;
VAR
Age : 0..100;
BEGIN
ClrScr;
WriteLn;
WriteLn;
Write ( 'How old are you? ' );
{$R-} { Turn Range Checking Off }
ReadLn ( Age );
IF (Age) > 100 THEN
WriteLn ('Age is too high. See value below and then re-enter. ')
ELSE
IF (Age) < 0 THEN
WriteLn ('Age is too low. See value below and then re-enter. ')
ELSE
{$R+} { Turn Range Checking On }
WriteLn;
WriteLn;
WriteLn ( 'You are ', Age, ' years old.' );
ReadLn;
END;
{-----------------------------}
{ VARCONST.PROC }
{-----------------------------}
PROCEDURE VarConst;
{ This repeats the lesson given by
Noe Lopez regarding Variable
Constants. See Tom Swan pp. 31-32
for the same thing.
A variable constant is a static
variable, or typed constant.
It allows the programmer to combine
constants and variables and save
memory. The programmer pre-initializes
a variable as a constant and then
change it.}
CONST
MiscString : String[10] = 'Lesson 2';
BEGIN
ClrScr; { This ClrScr clears the menu off the screen. }
WriteLn ( 'This is ', (MiscString) );
WriteLn;
WriteLn ( 'If not, please enter the correct Lesson.');
ReadLn ( MiscString );
WriteLn;
WriteLn ( 'The correct lesson is ',(MiscString),'.');
ReadLn; { This ReadLn keeps the program from
returning to the menu immediately.
Try modifying it by commenting out
the ReadLn, or deleting it. }
END;
{ MiscString is a constant that is
assigned a variable value. In the
course of the procedure, the user
interacts and the program reads in
the new value. }
{-----------------------------}
{ .PROC }
{-----------------------------}
PROCEDURE Null;
BEGIN
END;
{------------------------------------}
{ MAIN PROGRAM }
{------------------------------------}
BEGIN
Init; { Procedure Call }
UserQuits := False; { Boolean Variable set to false }
REPEAT { Note that REPEAT UNTIL contains all the
WriteLn statements of the Menu, then the
ReadLn statement to capture the input,
before the CASE END block. }
ClrScr;
WriteLn;
WriteLn ( ' Welcome to Beginners Pascal');
WriteLn;
WriteLn ( ' MENU');
WriteLn;
WriteLn ( ' A. Variable Constant ' );
WriteLn ( ' B. Range Check with Error Code ' );
WriteLn ( ' C. Range Check with Program Error Message ' );
WriteLn ( ' D. ASCII Code ' );
WriteLn ( ' Q. User Quits Menu' );
WriteLn;
WriteLn;
Write ( 'Select a menu choice: ' );
ReadLn ( Choice );
{ The CASE ___ OF statement uses the variable,
in this case "Choice" to capture the input,
then executes that procedure/function. }
{ Notice that after a choice, the loop returns
back to the Menu. }
CASE Choice OF
'a', 'A' : VarConst; { Choice A calls Procedure VarConst }
'b', 'B' : RangeChk; { Choice B calls Procedure RangeChk }
'c', 'C' : RangeChk2; { Etc. }
'd', 'D' : ASCII;
'e', 'E' : Null; { Choice calls Null.Proc until assigned }
'q', 'Q' : UserQuits := True; { Choice Q changes boolean value to False
and exits Repeat Until Loop and goes to
next line of Main Program code. That is,
in this program it stops repeating the
menu and drops the user to a cursor. When
you press <ENTER>, it exits the program.
Change the program to exit immediately.}
END
UNTIL UserQuits;
ReadLn;
END. { ReadLn stops the program for user }
{ input. This is a common }
{ way to get the program to stop and }
{ show you the screen without pres- }
{ sing ALT-F5. }